import 'package:app_30/res/gaps.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

void showSingleDialog(
  BuildContext context,
  String msg, {
  String title = '温馨提示',
  String positiveButtonText = '确定',
}) {
  showDialog<void>(
      context: context,
      barrierDismissible: false,
      builder: (BuildContext context) {
        return AlertDialog(
          title: Text('${title}'),
          content: Text('$msg'),
          actions: <Widget>[
            TextButton(
              onPressed: () => Navigator.of(context).pop(),
              style: ButtonStyle(
                overlayColor: MaterialStateProperty.all<Color>(
                    Theme.of(context).errorColor.withOpacity(0.2)),
              ),
              child: Text(
                '${positiveButtonText}',
                style: TextStyle(color: Theme.of(context).errorColor),
              ),
            ),
          ],
        );
      });

  void _close() {
    Navigator.of(context).pop();
  }
}

void showMultipleDialog(
  BuildContext context,
  String msg, {
  String title = '温馨提示',
  String negativeButtonText = "取消",
  String positiveButtonText = '确定',
  required VoidCallback positiveAction,
}) {
  showDialog<void>(
      context: context,
      barrierDismissible: false,
      builder: (BuildContext context) {
        return AlertDialog(
          title: Text('${title}'),
          content: Text('$msg'),
          actions: <Widget>[
            TextButton(
              onPressed: () => Navigator.of(context).pop(),
              child: Text('${negativeButtonText}'),
            ),
            TextButton(
              onPressed: positiveAction,
              style: ButtonStyle(
                overlayColor: MaterialStateProperty.all<Color>(
                    Theme.of(context).errorColor.withOpacity(0.2)),
              ),
              child: Text(
                '${positiveButtonText}',
                style: TextStyle(color: Theme.of(context).errorColor),
              ),
            ),
          ],
        );
      });

  void _close() {
    Navigator.of(context).pop();
  }
}

bool _isShowDialog = false;

@override
void showProgress(BuildContext context) {
  /// 避免重复弹出
  if (!_isShowDialog) {
    _isShowDialog = true;
    try {
      showDialog<void>(
        context: context,
        barrierDismissible: false,
        barrierColor: const Color(0x00FFFFFF),
        // 默认dialog背景色为半透明黑色，这里修改为透明（1.20添加属性）
        builder: (_) {
          return WillPopScope(
            onWillPop: () async {
              // 拦截到返回键，证明dialog被手动关闭
              _isShowDialog = false;
              return Future.value(true);
            },
            child: buildProgress(),
          );
        },
      );
    } catch (e) {
      /// 异常原因主要是页面没有build完成就调用Progress。
      print(e);
    }
  }
}

@override
void closeProgress(BuildContext context) {
  if (_isShowDialog) {
    _isShowDialog = false;
    Navigator.of(context).pop();
  }
}

/// 可自定义Progress
Widget buildProgress() => const ProgressDialog(hintText: '正在加载...');

/// 加载中的弹框
class ProgressDialog extends Dialog {
  const ProgressDialog({
    Key? key,
    this.hintText = '',
  }) : super(key: key);

  final String hintText;

  @override
  Widget build(BuildContext context) {
    final Widget progress = Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
        Theme(
          data: ThemeData(
            cupertinoOverrideTheme: const CupertinoThemeData(
              brightness: Brightness.dark, // 局部指定夜间模式，加载圈颜色会设置为白色
            ),
          ),
          child: const CupertinoActivityIndicator(radius: 14.0),
        ),
        Gaps.vGap8,
        Text(
          hintText,
          style: const TextStyle(color: Colors.white),
        )
      ],
    );

    return Material(
      type: MaterialType.transparency,
      child: Center(
        child: Container(
          height: 88.0,
          width: 120.0,
          decoration: const ShapeDecoration(
            color: Color(0xFF3A3A3A),
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.all(Radius.circular(8.0)),
            ),
          ),
          child: progress,
        ),
      ),
    );
  }
}
